/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x10.event;

import java.io.Serializable;

import x10.enums.Device;
import x10.enums.House;
import x10.enums.Remote;

/**
 *
 * @author Denny
 */
public class X10Remote extends X10Event implements Serializable {
    static final long serialVersionUID = 1L;
    House houseCode;
    Device deviceCode;
    Remote remoteCommand;
    private String address;
    private String source;
    private long instantiatedMillis;

    public X10Remote(House houseCode, Device deviceCode, Remote command, String source){
	this.instantiatedMillis = System.currentTimeMillis();
        this.address = houseCode.toString() + deviceCode.toString();
        this.houseCode = houseCode;
        this.deviceCode = deviceCode;
        this.remoteCommand = command;
        this.source = source;
    }

    /**
     * @return the source
     */
    public String getSource() {
        return source;
    }

    /**
     * @param source the source to set
     */
    public void setSource(String source) {
        this.source = source;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the instantiatedMillis
     */
    public long getInstantiatedMillis() {
        return instantiatedMillis;
    }

    /**
     * @param instantiatedMillis the instantiatedMillis to set
     */
    public void setInstantiatedMillis(long instantiatedMillis) {
        this.instantiatedMillis = instantiatedMillis;
    }

    public House getHouseCode() {
        return houseCode;
    }

    public void setHouseCode(House houseCode) {
        this.houseCode = houseCode;
    }

    public Device getDeviceCode() {
        return deviceCode;
    }

    public void setDeviceCode(Device deviceCode) {
        this.deviceCode = deviceCode;
    }

    public byte getAddressCode(){
        //Form a byte address code representing house in upper nibble and device in lower
        byte house = houseCode.getRadioFrequencyHouseCodeInUpperNibble();
        byte device = deviceCode.getOrdinal();
        return (byte)(house | device);
    }

    public Remote getRemoteCommand() {
        return remoteCommand;
    }

    public void setRemoteCommand(Remote command) {
        this.remoteCommand = command;
    }

    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("X10 Remote ");
        sb.append("Address:");
        sb.append(address);
        sb.append(" ");
        sb.append("Command:");
        sb.append(getRemoteCommand());
        sb.append(" ");
        sb.append("Source:");
        sb.append(getSource());
        return sb.toString();
    }

    public boolean equals(X10Event device){
        boolean isEqual = true;

        if(device instanceof X10Remote){
            X10Remote other = (X10Remote)device;
            if(other!=null){
                if(!this.getAddress().contentEquals(other.getAddress())){
                    isEqual = false;
                }
                if(!this.getSource().contentEquals(other.getSource())){
                    isEqual = false;
                }
                if(this.houseCode != other.getHouseCode()){
                    isEqual = false;
                }
                if(this.deviceCode != other.getDeviceCode()){
                    isEqual = false;
                }
                if(this.remoteCommand != other.getRemoteCommand()){
                    isEqual = false;
                }

                //If both objects are within 1 second of each other, call them equal
                long low = this.getInstantiatedMillis() - 1000; //1 second earlier
                long high = this.getInstantiatedMillis() + 1000; //1 second later
                long otherMillis = other.getInstantiatedMillis();

                if(!((otherMillis >= low) && (otherMillis <= high))){
                    isEqual = false;
                }
            }
        } else {
            isEqual = false;
        }

        return isEqual;
    }
}
